﻿package 
{
	//flash
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextField;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.utils.getDefinitionByName;
	import flash.media.SoundMixer;
	//pureMVC
	//custom
	import com.tvein.Web;
	import com.tvein.web.swc.logo.Tvein;
//
	public class preWeb extends MovieClip
	{
		private var _logo:Tvein = new Tvein();
		private var _txtMsg:TextField = new TextField();
		
		public function preWeb()
		{
			addEventListener(Event.ENTER_FRAME, checkFrame);
			
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
			
			//show loader
			_logo.x = stage.stageWidth / 2 - _logo.width / 2;
			_logo.y = stage.stageHeight / 2 - _logo.height / 2;
			_txtMsg.text = "Loading...";
			_txtMsg.textColor = 0x898989;
			_txtMsg.x = stage.stageWidth / 2 - _txtMsg.width / 2;
			_txtMsg.y = stage.stageHeight / 2 + _logo.height / 2;
			addChild(_logo);
			addChild(_txtMsg);
		}
		
		private function progress(e:ProgressEvent):void
		{
			//update loader
			_txtMsg.text = "Loading..." + Math.floor(e.bytesLoaded / e.bytesTotal * 100) + "%";
		}
		
		private function checkFrame(e:Event):void 
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			if (currentFrame == totalFrames)
			{
				removeEventListener(Event.ENTER_FRAME, checkFrame);
				startup();
			}
		}
		
		public function startup():void
		{
			SoundMixer.stopAll();
			
			for (var i:int = this.numChildren - 1; i >= 0; i--)
			{
				this.removeChildAt(0); 
			}
			
			com.tvein.Web;
			var mainClass:Class = getDefinitionByName("com.tvein.Web") as Class;
			addChild(new mainClass() as DisplayObject);
		}
	}
}